package com.magic.gre.mvp.presenter;

import com.google.gson.reflect.TypeToken;
import com.magic.gre.entity.UserBean;
import com.magic.gre.entity.base.BaseObjectModel;
import com.magic.gre.helper.Apphelper;
import com.magic.gre.mvp.contract.ModifyPhoneContract;
import com.magic.gre.mvp.model.ModifyPhoneModelImpl;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.mvp.BasePresenterImpl;
import com.noname.lib_base_java.net.RxBus;

/**
 * Author: Lmoumou
 * Date: 2019-01-11 11:30
 * 文件名: ModifyPhonepresenterImpl
 * 描述:
 */
public class ModifyPhonepresenterImpl extends BasePresenterImpl<ModifyPhoneContract.View, ModifyPhoneContract.Model>
        implements ModifyPhoneContract.Presenter {
    public ModifyPhonepresenterImpl(ModifyPhoneContract.View view) {
        super(view);
    }

    @Override
    protected ModifyPhoneContract.Model creatModel() {
        return new ModifyPhoneModelImpl();
    }

    @Override
    public void pSmsCode(String phone,int type) {
        mModel.mSmsCode(new CommonObserver<BaseObjectModel<String>>(new TypeToken<BaseObjectModel<String>>(){}.getType()) {
            @Override
            protected void onSuccess(BaseObjectModel<String> stringBaseObjectModel) {
                mView.vSmsCode(stringBaseObjectModel.body);

            }

            @Override
            public void onError(int status, String msg) {
                mView.doPrompt(msg);
                mView.hideLoadDialog();
            }
        },phone,type);
    }

    @Override
    public void pChangePhone(String phone, String securityCode) {
        mModel.mChangePhone(new CommonObserver<BaseObjectModel<UserBean>>(new TypeToken<BaseObjectModel<UserBean>>(){}.getType()) {
            @Override
            protected void onSuccess(BaseObjectModel<UserBean> baseObjectModel) {
                Apphelper.putToken(baseObjectModel.body.getToken());
                Apphelper.putUserInfo(baseObjectModel.body);
                RxBus.getInstance().post(new MsgEvent(MsgEvent.UPDATE_PER));
                mView.vChangePhone();
            }

            @Override
            public void onError(int status, String msg) {
                mView.doPrompt(msg);
            }
        }, phone, securityCode);
    }
}
